package com.lp.business.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.iframework.commons.domain.enums.RecordStatus;
import org.iframework.commons.domain.order.Order;
import org.iframework.commons.domain.pager.Pager;
import org.iframework.commons.util.fast.V;
import org.iframework.support.spring.hibernate.dao.BaseHibernateDaoSupport;
import org.iframework.support.spring.hibernate.service.BaseServiceSupport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.lp.business.dao.SubscriptionDao;
import com.lp.business.model.Industry;
import com.lp.business.model.Subscription;
import com.lp.business.service.IndustryService;
import com.lp.business.service.SubscriptionService;

/**
 * 订阅信息模型服务接口实现类<br>
 * 说明：支持方法查阅实现接口
 * 
 * @author lp
 * @email lp@qq.com
 * @date 2018-08-23 10:38:18
 */
@Service("subscriptionService")
public class SubscriptionServiceImpl extends BaseServiceSupport<Subscription, String> implements SubscriptionService {
	@Autowired
	private IndustryService industryService;
	@Resource(name = "subscriptionDao")
	public void setBaseHibernateDaoSupport(@Qualifier("subscriptionDaoImpl") BaseHibernateDaoSupport<Subscription, String> baseHibernateDaoSupport) {
		this.baseHibernateDaoSupport = baseHibernateDaoSupport;
	}

	@Override
	public boolean isSubscribed(String memberId,String industryName) {
		if(V.isEmpty(memberId)||V.isEmpty(industryName)) {
			return false;
		}
		Industry industry = industryService.findByName(industryName);
		if(V.isEmpty(industry)) {
			return false;
		}
		Subscription model = new Subscription();
		model.setStatus(RecordStatus.AVAILABLE);
		model.setMemberId(memberId);
		model.setIndustryId(industry.getId());
		model.setExpiryDate(new Date());
		List<Subscription> models = this.findByModel(model, null, null);
		return V.isNotEmpty(models);
	}

	@Override
	public List<Map<String, Object>> findSubscriptions(Subscription model, Order order, Pager pager) {
		return ((SubscriptionDao)this.baseHibernateDaoSupport).findSubscriptions(model, order, pager);
	}
	
	
}